<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------
// 应用公共文件
function tree($list, $id_name, $alias = [], $child = 'children')
{
    $build = [];
    foreach ($list as $item) {
        foreach ($alias as $key => $items) {
            if (isset($item[$key])) {
                $build[$item[$id_name]][$items] = $item[$key];
            } else {
                // 附加字段
                $build[$item[$id_name]][$key] = $items;
            }
        }
    }
    $primary_key = isset($alias[$id_name]) ? $alias[$id_name] : $id_name;
    $tree = array();
    foreach ($build as $item) {
        if (isset($build[$item['pid']])) {
            $build[$item['pid']][$child][] = &$build[$item[$primary_key]];
        } else {
            $tree[] = &$build[$item[$primary_key]];
        }
    }
    return $tree;
}

function _create_sign($path, $param)
{
    $common ['timestamp'] = time();
    $common ['ver'] = config('lieliu.version');
    $common ['username'] = config('lieliu.username');
    $common ['id'] = date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    $common ['format'] = 'json';
    $param = array_merge($common, $param);
    $token = config('lieliu.key');
    ksort($param);
    $encode = http_build_query($param);
    $encode_after = $path . '?' . urldecode($encode) . '&' . $token;
    $encode = urlencode($encode_after);
    $md5 = md5($encode);
    $content = '';
    foreach ($param as $key => $val) {
        $content .= sprintf('%s=%s&', $key, urlencode($val));
    }
    $content .= sprintf('signkey=%s', $md5);
    return $content;
}

function curl_request($url, $method = 'get', $data = null, $https = true)
{
    //1.初识化curl
    $result = \GuzzleHttp\Psr7\parse_query($data);
    $param = explode('&', $data);
    $query = [];
    array_map(function ($val) use (&$query) {
        $v = explode('=', $val);
        $query[$v[0]] = $v[1];
    }, $param);
    $ch = curl_init($url);
    //2.根据实际请求需求进行参数封装
    //返回数据不直接输出
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //如果是https请求
    if ($https === true) {
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    }
    //如果是post请求


    if ($method === 'post') {
        //开启发送post请求选项
        curl_setopt($ch, CURLOPT_POST, true);
        //发送post的数据
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    //3.发送请求
    $result = curl_exec($ch);
    if (!$result) {
        $this->encode_json('4004', 'network error !');
    }
    $data = json_decode($result, true);
    $data['order_id'] = $query['id'];
    //4.返回返回值，关闭连接
    curl_close($ch);
    return $data;
}
/**
 * 判断字符串是否为 Json 格式
 *
 * @param string $data Json 字符串
 * @param bool $assoc 是否返回关联数组。默认返回对象
 *
 * @return array|bool|object 成功返回转换后的对象或数组，失败返回 false
 */
function isJson($data = '', $assoc = false)
{
    $data = json_decode($data,$assoc);
    if(($data && is_object($data)) || (is_array($data) && !empty($data))){
        return$data;
    }return false;
}
